<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>progressbar.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_progressbar.js.html">progressbar.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Implementation of a progress bar.
<a name="line17"></a> *
<a name="line18"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line19"></a> * @see ../demos/progressbar.html
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.ProgressBar&#39;);
<a name="line24"></a>goog.provide(&#39;goog.ui.ProgressBar.Orientation&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.a11y.aria&#39;);
<a name="line27"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom&#39;);
<a name="line29"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line30"></a>goog.require(&#39;goog.events&#39;);
<a name="line31"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line34"></a>goog.require(&#39;goog.ui.RangeModel&#39;);
<a name="line35"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * This creates a progress bar object.
<a name="line41"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
<a name="line42"></a> * @constructor
<a name="line43"></a> * @extends {goog.ui.Component}
<a name="line44"></a> */
<a name="line45"></a>goog.ui.ProgressBar = function(opt_domHelper) {
<a name="line46"></a>  goog.ui.Component.call(this, opt_domHelper);
<a name="line47"></a>
<a name="line48"></a>  /**
<a name="line49"></a>   * The underlying data model for the progress bar.
<a name="line50"></a>   * @type {goog.ui.RangeModel}
<a name="line51"></a>   * @private
<a name="line52"></a>   */
<a name="line53"></a>  this.rangeModel_ = new goog.ui.RangeModel;
<a name="line54"></a>  goog.events.listen(this.rangeModel_, goog.ui.Component.EventType.CHANGE,
<a name="line55"></a>                     this.handleChange_, false, this);
<a name="line56"></a>};
<a name="line57"></a>goog.inherits(goog.ui.ProgressBar, goog.ui.Component);
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Enum for representing the orientation of the progress bar.
<a name="line62"></a> *
<a name="line63"></a> * @enum {string}
<a name="line64"></a> */
<a name="line65"></a>goog.ui.ProgressBar.Orientation = {
<a name="line66"></a>  VERTICAL: &#39;vertical&#39;,
<a name="line67"></a>  HORIZONTAL: &#39;horizontal&#39;
<a name="line68"></a>};
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Map from progress bar orientation to CSS class names.
<a name="line73"></a> * @type {Object}
<a name="line74"></a> * @private
<a name="line75"></a> */
<a name="line76"></a>goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_ = {};
<a name="line77"></a>goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[
<a name="line78"></a>    goog.ui.ProgressBar.Orientation.VERTICAL] =
<a name="line79"></a>        goog.getCssName(&#39;progress-bar-vertical&#39;);
<a name="line80"></a>goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[
<a name="line81"></a>    goog.ui.ProgressBar.Orientation.HORIZONTAL] =
<a name="line82"></a>        goog.getCssName(&#39;progress-bar-horizontal&#39;);
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Creates the DOM nodes needed for the progress bar
<a name="line87"></a> * @override
<a name="line88"></a> */
<a name="line89"></a>goog.ui.ProgressBar.prototype.createDom = function() {
<a name="line90"></a>  this.thumbElement_ = this.createThumb_();
<a name="line91"></a>  var cs = goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[this.orientation_];
<a name="line92"></a>  this.setElementInternal(
<a name="line93"></a>      this.getDomHelper().createDom(&#39;div&#39;, cs, this.thumbElement_));
<a name="line94"></a>  this.setValueState_();
<a name="line95"></a>  this.setMinimumState_();
<a name="line96"></a>  this.setMaximumState_();
<a name="line97"></a>};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/** @override */
<a name="line101"></a>goog.ui.ProgressBar.prototype.enterDocument = function() {
<a name="line102"></a>  goog.ui.ProgressBar.superClass_.enterDocument.call(this);
<a name="line103"></a>  this.attachEvents_();
<a name="line104"></a>  this.updateUi_();
<a name="line105"></a>
<a name="line106"></a>  var element = this.getElement();
<a name="line107"></a>  goog.asserts.assert(element,
<a name="line108"></a>      &#39;The progress bar DOM element cannot be null.&#39;);
<a name="line109"></a>  // state live = polite will notify the user of updates,
<a name="line110"></a>  // but will not interrupt ongoing feedback
<a name="line111"></a>  goog.a11y.aria.setRole(element, &#39;progressbar&#39;);
<a name="line112"></a>  goog.a11y.aria.setState(element, &#39;live&#39;, &#39;polite&#39;);
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/** @override */
<a name="line117"></a>goog.ui.ProgressBar.prototype.exitDocument = function() {
<a name="line118"></a>  goog.ui.ProgressBar.superClass_.exitDocument.call(this);
<a name="line119"></a>  this.detachEvents_();
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * This creates the thumb element.
<a name="line125"></a> * @private
<a name="line126"></a> * @return {HTMLDivElement} The created thumb element.
<a name="line127"></a> */
<a name="line128"></a>goog.ui.ProgressBar.prototype.createThumb_ = function() {
<a name="line129"></a>  return /** @type {HTMLDivElement} */ (this.getDomHelper().createDom(&#39;div&#39;,
<a name="line130"></a>      goog.getCssName(&#39;progress-bar-thumb&#39;)));
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Adds the initial event listeners to the element.
<a name="line136"></a> * @private
<a name="line137"></a> */
<a name="line138"></a>goog.ui.ProgressBar.prototype.attachEvents_ = function() {
<a name="line139"></a>  if (goog.userAgent.IE &amp;&amp; goog.userAgent.VERSION &lt; 7) {
<a name="line140"></a>    goog.events.listen(this.getElement(), goog.events.EventType.RESIZE,
<a name="line141"></a>                       this.updateUi_, false, this);
<a name="line142"></a>  }
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * Removes the event listeners added by attachEvents_.
<a name="line148"></a> * @private
<a name="line149"></a> */
<a name="line150"></a>goog.ui.ProgressBar.prototype.detachEvents_ = function() {
<a name="line151"></a>  if (goog.userAgent.IE &amp;&amp; goog.userAgent.VERSION &lt; 7) {
<a name="line152"></a>    goog.events.unlisten(this.getElement(), goog.events.EventType.RESIZE,
<a name="line153"></a>                         this.updateUi_, false, this);
<a name="line154"></a>  }
<a name="line155"></a>};
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * Decorates an existing HTML DIV element as a progress bar input. If the
<a name="line160"></a> * element contains a child with a class name of &#39;progress-bar-thumb&#39; that will
<a name="line161"></a> * be used as the thumb.
<a name="line162"></a> * @param {Element} element  The HTML element to decorate.
<a name="line163"></a> * @override
<a name="line164"></a> */
<a name="line165"></a>goog.ui.ProgressBar.prototype.decorateInternal = function(element) {
<a name="line166"></a>  goog.ui.ProgressBar.superClass_.decorateInternal.call(this, element);
<a name="line167"></a>  goog.dom.classes.add(this.getElement(), goog.ui.ProgressBar.
<a name="line168"></a>      ORIENTATION_TO_CSS_NAME_[this.orientation_]);
<a name="line169"></a>
<a name="line170"></a>  // find thumb
<a name="line171"></a>  var thumb = goog.dom.getElementsByTagNameAndClass(
<a name="line172"></a>      null, goog.getCssName(&#39;progress-bar-thumb&#39;), this.getElement())[0];
<a name="line173"></a>  if (!thumb) {
<a name="line174"></a>    thumb = this.createThumb_();
<a name="line175"></a>    this.getElement().appendChild(thumb);
<a name="line176"></a>  }
<a name="line177"></a>  this.thumbElement_ = thumb;
<a name="line178"></a>};
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * @return {number} The value.
<a name="line183"></a> */
<a name="line184"></a>goog.ui.ProgressBar.prototype.getValue = function() {
<a name="line185"></a>  return this.rangeModel_.getValue();
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Sets the value
<a name="line191"></a> * @param {number} v The value.
<a name="line192"></a> */
<a name="line193"></a>goog.ui.ProgressBar.prototype.setValue = function(v) {
<a name="line194"></a>  this.rangeModel_.setValue(v);
<a name="line195"></a>  if (this.getElement()) {
<a name="line196"></a>    this.setValueState_();
<a name="line197"></a>  }
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Sets the state for a11y of the current value.
<a name="line203"></a> * @private
<a name="line204"></a> */
<a name="line205"></a>goog.ui.ProgressBar.prototype.setValueState_ = function() {
<a name="line206"></a>  var element = this.getElement();
<a name="line207"></a>  goog.asserts.assert(element,
<a name="line208"></a>      &#39;The progress bar DOM element cannot be null.&#39;);
<a name="line209"></a>  goog.a11y.aria.setState(element, &#39;valuenow&#39;, this.getValue());
<a name="line210"></a>};
<a name="line211"></a>
<a name="line212"></a>
<a name="line213"></a>/**
<a name="line214"></a> * @return {number} The minimum value.
<a name="line215"></a> */
<a name="line216"></a>goog.ui.ProgressBar.prototype.getMinimum = function() {
<a name="line217"></a>  return this.rangeModel_.getMinimum();
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/**
<a name="line222"></a> * Sets the minimum number
<a name="line223"></a> * @param {number} v The minimum value.
<a name="line224"></a> */
<a name="line225"></a>goog.ui.ProgressBar.prototype.setMinimum = function(v) {
<a name="line226"></a>  this.rangeModel_.setMinimum(v);
<a name="line227"></a>  if (this.getElement()) {
<a name="line228"></a>    this.setMinimumState_();
<a name="line229"></a>  }
<a name="line230"></a>};
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * Sets the state for a11y of the minimum value.
<a name="line235"></a> * @private
<a name="line236"></a> */
<a name="line237"></a>goog.ui.ProgressBar.prototype.setMinimumState_ = function() {
<a name="line238"></a>  var element = this.getElement();
<a name="line239"></a>  goog.asserts.assert(element,
<a name="line240"></a>       &#39;The progress bar DOM element cannot be null.&#39;);
<a name="line241"></a>  goog.a11y.aria.setState(element, &#39;valuemin&#39;, this.getMinimum());
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>/**
<a name="line246"></a> * @return {number} The maximum value.
<a name="line247"></a> */
<a name="line248"></a>goog.ui.ProgressBar.prototype.getMaximum = function() {
<a name="line249"></a>  return this.rangeModel_.getMaximum();
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * Sets the maximum number
<a name="line255"></a> * @param {number} v The maximum value.
<a name="line256"></a> */
<a name="line257"></a>goog.ui.ProgressBar.prototype.setMaximum = function(v) {
<a name="line258"></a>  this.rangeModel_.setMaximum(v);
<a name="line259"></a>  if (this.getElement()) {
<a name="line260"></a>    this.setMaximumState_();
<a name="line261"></a>  }
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Sets the state for a11y of the maximum valiue.
<a name="line267"></a> * @private
<a name="line268"></a> */
<a name="line269"></a>goog.ui.ProgressBar.prototype.setMaximumState_ = function() {
<a name="line270"></a>  var element = this.getElement();
<a name="line271"></a>  goog.asserts.assert(element,
<a name="line272"></a>      &#39;The progress bar DOM element cannot be null.&#39;);
<a name="line273"></a>  goog.a11y.aria.setState(element, &#39;valuemax&#39;, this.getMaximum());
<a name="line274"></a>};
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>/**
<a name="line278"></a> *
<a name="line279"></a> * @type {goog.ui.ProgressBar.Orientation}
<a name="line280"></a> * @private
<a name="line281"></a> */
<a name="line282"></a>goog.ui.ProgressBar.prototype.orientation_ =
<a name="line283"></a>    goog.ui.ProgressBar.Orientation.HORIZONTAL;
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * Call back when the internal range model changes
<a name="line288"></a> * @param {goog.events.Event} e The event object.
<a name="line289"></a> * @private
<a name="line290"></a> */
<a name="line291"></a>goog.ui.ProgressBar.prototype.handleChange_ = function(e) {
<a name="line292"></a>  this.updateUi_();
<a name="line293"></a>  this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * This is called when we need to update the size of the thumb. This happens
<a name="line299"></a> * when first created as well as when the value and the orientation changes.
<a name="line300"></a> * @private
<a name="line301"></a> */
<a name="line302"></a>goog.ui.ProgressBar.prototype.updateUi_ = function() {
<a name="line303"></a>  if (this.thumbElement_) {
<a name="line304"></a>    var min = this.getMinimum();
<a name="line305"></a>    var max = this.getMaximum();
<a name="line306"></a>    var val = this.getValue();
<a name="line307"></a>    var ratio = (val - min) / (max - min);
<a name="line308"></a>    var size = Math.round(ratio * 100);
<a name="line309"></a>    if (this.orientation_ == goog.ui.ProgressBar.Orientation.VERTICAL) {
<a name="line310"></a>      // Note(arv): IE up to version 6 has some serious computation bugs when
<a name="line311"></a>      // using percentages or bottom. We therefore first set the height to
<a name="line312"></a>      // 100% and measure that and base the top and height on that size instead.
<a name="line313"></a>      if (goog.userAgent.IE &amp;&amp; goog.userAgent.VERSION &lt; 7) {
<a name="line314"></a>        this.thumbElement_.style.top = 0;
<a name="line315"></a>        this.thumbElement_.style.height = &#39;100%&#39;;
<a name="line316"></a>        var h = this.thumbElement_.offsetHeight;
<a name="line317"></a>        var bottom = Math.round(ratio * h);
<a name="line318"></a>        this.thumbElement_.style.top = h - bottom + &#39;px&#39;;
<a name="line319"></a>        this.thumbElement_.style.height = bottom + &#39;px&#39;;
<a name="line320"></a>      } else {
<a name="line321"></a>        this.thumbElement_.style.top = (100 - size) + &#39;%&#39;;
<a name="line322"></a>        this.thumbElement_.style.height = size + &#39;%&#39;;
<a name="line323"></a>      }
<a name="line324"></a>    } else {
<a name="line325"></a>      this.thumbElement_.style.width = size + &#39;%&#39;;
<a name="line326"></a>    }
<a name="line327"></a>  }
<a name="line328"></a>};
<a name="line329"></a>
<a name="line330"></a>
<a name="line331"></a>/**
<a name="line332"></a> * This is called when we need to setup the UI sizes and positions. This
<a name="line333"></a> * happens when we create the element and when we change the orientation.
<a name="line334"></a> * @private
<a name="line335"></a> */
<a name="line336"></a>goog.ui.ProgressBar.prototype.initializeUi_ = function() {
<a name="line337"></a>  var tStyle = this.thumbElement_.style;
<a name="line338"></a>  if (this.orientation_ == goog.ui.ProgressBar.Orientation.VERTICAL) {
<a name="line339"></a>    tStyle.left = 0;
<a name="line340"></a>    tStyle.width = &#39;100%&#39;;
<a name="line341"></a>  } else {
<a name="line342"></a>    tStyle.top = tStyle.left = 0;
<a name="line343"></a>    tStyle.height = &#39;100%&#39;;
<a name="line344"></a>  }
<a name="line345"></a>};
<a name="line346"></a>
<a name="line347"></a>
<a name="line348"></a>/**
<a name="line349"></a> * Changes the orientation
<a name="line350"></a> * @param {goog.ui.ProgressBar.Orientation} orient The orientation.
<a name="line351"></a> */
<a name="line352"></a>goog.ui.ProgressBar.prototype.setOrientation = function(orient) {
<a name="line353"></a>  if (this.orientation_ != orient) {
<a name="line354"></a>    var oldCss =
<a name="line355"></a>        goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[this.orientation_];
<a name="line356"></a>    var newCss = goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[orient];
<a name="line357"></a>    this.orientation_ = orient;
<a name="line358"></a>
<a name="line359"></a>    // Update the DOM
<a name="line360"></a>    if (this.getElement()) {
<a name="line361"></a>      goog.dom.classes.swap(this.getElement(), oldCss, newCss);
<a name="line362"></a>      this.initializeUi_();
<a name="line363"></a>      this.updateUi_();
<a name="line364"></a>    }
<a name="line365"></a>  }
<a name="line366"></a>};
<a name="line367"></a>
<a name="line368"></a>
<a name="line369"></a>/**
<a name="line370"></a> * @return {goog.ui.ProgressBar.Orientation} The orientation of the
<a name="line371"></a> *     progress bar.
<a name="line372"></a> */
<a name="line373"></a>goog.ui.ProgressBar.prototype.getOrientation = function() {
<a name="line374"></a>  return this.orientation_;
<a name="line375"></a>};
<a name="line376"></a>
<a name="line377"></a>
<a name="line378"></a>/** @override */
<a name="line379"></a>goog.ui.ProgressBar.prototype.disposeInternal = function() {
<a name="line380"></a>  this.detachEvents_();
<a name="line381"></a>  goog.ui.ProgressBar.superClass_.disposeInternal.call(this);
<a name="line382"></a>  this.thumbElement_ = null;
<a name="line383"></a>  this.rangeModel_.dispose();
<a name="line384"></a>};
<a name="line385"></a>
<a name="line386"></a>
<a name="line387"></a>/**
<a name="line388"></a> * @return {?number} The step value used to determine how to round the value.
<a name="line389"></a> */
<a name="line390"></a>goog.ui.ProgressBar.prototype.getStep = function() {
<a name="line391"></a>  return this.rangeModel_.getStep();
<a name="line392"></a>};
<a name="line393"></a>
<a name="line394"></a>
<a name="line395"></a>/**
<a name="line396"></a> * Sets the step value. The step value is used to determine how to round the
<a name="line397"></a> * value.
<a name="line398"></a> * @param {?number} step  The step size.
<a name="line399"></a> */
<a name="line400"></a>goog.ui.ProgressBar.prototype.setStep = function(step) {
<a name="line401"></a>  this.rangeModel_.setStep(step);
<a name="line402"></a>};
</pre>


</body>
</html>
